package com.xnyzc.lhy.mis.controller.cmsapi.user.wallet;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.cms.user.wallet.UpdateMoneyParam;
import com.xnyzc.lhy.mis.entity.param.cms.user.wallet.UpdateSwitchParam;
import com.xnyzc.lhy.mis.service.cms.user.wallet.IOaSysUserWalletService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;

/**
 * @Description 用户钱包管理
 * @Author zhuyubin
 * @Date 2019-08-20 16:13
 **/
@RestController
@RequestMapping(value = "cmsapi/userWallet")
public class OaSysUserWalletController {

    @Autowired
    private IOaSysUserWalletService iOaSysUserWalletService;

    @GetMapping(value = "seleceOaSysUserWallet")
    public Rv seleceOaSysUserWallet(
            @ApiParam(name = "current", value = "当前页") Long current,
            @ApiParam(name = "name", value = "用户名") @RequestParam(required = false) String name,
            @ApiParam(name = "phone", value = "手机号") @RequestParam(required = false) String phone,
            @ApiParam(name = "officeId", value = "机构id") @RequestParam(required = false) Long officeId
    ){
        Long userId = TokenUtil.getTokenUserId();
        // TODO 暂时不做校验
        List<String> roleDataScope = TokenUtil.getRoleDataScope();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return Rv.wrap(iOaSysUserWalletService.seleceOaSysUserWallet(userId, roleDataScope, tag, current, name, phone, officeId));
    }

    /**
     * 是否开启额度
     * @return
     */
    @PostMapping(value = "updateSwitch")
    public Rv updateSwitch(@ApiParam(name = "updateSwitchParam", value = "入参") @RequestBody UpdateSwitchParam updateSwitchParam){
        Long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        List<Long> userIds = updateSwitchParam.getUserIds();
        Integer type = updateSwitchParam.getType();
        Integer switchType = updateSwitchParam.getSwitchType();
        if (CheckUtil.objIsEmpty(userIds, type, switchType)){
            throw PangException.create(EErrorCode.missingArg);
        }
        // return Rv.wrap(EErrorCode.success, iOaSysUserWalletService.updateSwitch(sysUserId, userIds, type, switchType, tag), new HashMap<>(16));
        return Rv.wrap(EErrorCode.success, "", new HashMap<>(16));
    }

    /**
     * 后台用户充值
     * @param updateMoneyParam
     * @return
     */
    @PostMapping(value = "updateMoney")
    public Rv updateMoney(@ApiParam(name = "updateMoneyParam", value = "入参") @RequestBody UpdateMoneyParam updateMoneyParam){
        Long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        Integer type = updateMoneyParam.getType();
        BigDecimal money = updateMoneyParam.getMoney();
        List<Long> userIds = updateMoneyParam.getUserIds();
        if (CheckUtil.objIsEmpty(userIds, type, money)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(EErrorCode.success, iOaSysUserWalletService.updateMoney(sysUserId, userIds, tag, type, money), new HashMap<>(16));
    }

    /**
     * 获取用户钱包
     * @param current 当前页
     * @param name 姓名
     * @return
     */
    @GetMapping(value = "selectUserWalletRecordPage")
    public Rv selectUserWalletRecordPage(
            @ApiParam(name = "current", value = "当前页", required = true) Long current,
            @ApiParam(name = "size", value = "每页条数") Long size,
            @ApiParam(name = "name", value = "名称") String name,
            @ApiParam(name = "phone", value = "手机号") String phone,
            @ApiParam(name = "orderNo", value = "订单号") String orderNo,
            @ApiParam(name = "recordType", value = "交易类型") @RequestParam(required = false) Integer recordType){
        long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        // 暂时不做校验
        List<String> roleDataScope = TokenUtil.getRoleDataScope();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return Rv.wrap(iOaSysUserWalletService.selectUserWalletRecordPage(sysUserId, roleDataScope, current, size, tag, name, phone, orderNo, recordType));
    }

    /**
     * 获取全部交易类型
     * @return
     */
    @GetMapping(value = "/selectRecordTypeAll")
    public Rv selectRecordTypeAll(){
        String tag = TokenUtil.getTag();
        if (CheckUtil.strIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return Rv.wrap(iOaSysUserWalletService.selectRecordTypeAll(tag));
    }
}
